package cn.com.xf.utils;

import java.nio.charset.Charset;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.http.Consts;

public class imgURLUtil {
	public static Charset DEFAULT_CHARSET = Consts.UTF_8;
	
	/**
	 * md5加密并且Base64
	 * 
	 * @param byteContent
	 * @return
	 * @throws Exception
	 */
	public static byte[] encodeMd5(String content) throws Exception {
		byte[] bytesMd5 = DigestUtils.md5(getBytesUtf8(content));
		return Base64.encodeBase64(bytesMd5);
	}

	private static byte[] getBytes(final String content, final Charset charset) {
		if (content == null) {
			return null;
		}
		return content.getBytes(charset);
	}

	private static String newString(final byte[] bytes, final Charset charset) {
		return bytes == null ? null : new String(bytes, charset);
	}

	public static byte[] getBytesUtf8(final String content) {
		return getBytes(content, DEFAULT_CHARSET);
	}

	public static String newStringUtf8(final byte[] bytes) {
		return newString(bytes, DEFAULT_CHARSET);
	}
	/**
	 * 空字符串
	 * 
	 * @param cs
	 * @return
	 */
	public static boolean isEmpty(final CharSequence cs) {
		return cs == null || cs.length() == 0;
	}

	/**
	 * 空字符串或内容为空格、换行、tab
	 * 
	 * @param cs
	 * @return
	 */
	public static boolean isBlank(final CharSequence cs) {
		int strLen;
		if (cs == null || (strLen = cs.length()) == 0) {
			return true;
		}
		for (int i = 0; i < strLen; i++) {
			if (Character.isWhitespace(cs.charAt(i)) == false) {
				return false;
			}
		}
		return true;
	}

	public static boolean isEmptyText(final CharSequence cs) {
		if (cs == null) {
			return false;
		}
		if (cs.length() == 0) {
			return true;
		}
		return false;
	}

	public static boolean isNotBlank(final CharSequence cs) {
		return !isBlank(cs);
	}
}
